﻿

/*
* Project: Sherp
* Copyright(c)
* Version: 1.0.0 
* Author: Caijianfeng <sharewind@gmail.com> 
* DateTime: 2007年6月2日 星期六 下午 15:17:40
*/



package com.cse.sherp.business.xitong;

import org.tulip.ProjectLog;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.tulip.DaoException;
import org.tulip.RootDao;
import org.tulip.util.BeanUtil;

/**
 * 工艺Dao
 */
public class GongYiDao extends RootDao {
	/**
	 * Logger for this class
	 */
	private static final ProjectLog logger = ProjectLog.getLogger(GongYiDao.class);

	@Override
	protected Object convert(ResultSet rs, List list) throws SQLException {
		GongYi current = new GongYi();
		if (list.contains("GY_ID"))
			current.setGyId(rs.getLong("GY_ID"));
		if (list.contains("NAME"))
			current.setName(rs.getString ("NAME"));
		if (list.contains("GYLB_ID"))
			current.setGylbId(rs.getLong("GYLB_ID"));
		if (list.contains("DW_ID"))
			current.setDwId(rs.getLong("DW_ID"));
		if (list.contains("PRICE"))
			current.setPrice(rs.getDouble("PRICE"));
		if (list.contains("QIDIAN_NUM"))
			current.setQidianNum(rs.getDouble("QIDIAN_NUM"));
		if (list.contains("QIDIAN_PRICE"))
			current.setQidianPrice(rs.getDouble("QIDIAN_PRICE"));
		if (list.contains("MAX_KD"))
			current.setMaxKd(rs.getInt("MAX_KD"));
		if (list.contains("MAX_SIZE_LENGTH"))
			current.setMaxSizeLength(rs.getDouble("MAX_SIZE_LENGTH"));
		if (list.contains("MAX_SIZE_WIDTH"))
			current.setMaxSizeWidth(rs.getDouble("MAX_SIZE_WIDTH"));
		if (list.contains("MIN_SIZE_LENGTH"))
			current.setMinSizeLength(rs.getDouble("MIN_SIZE_LENGTH"));
		if (list.contains("MIN_SIZE_WIDTH"))
			current.setMinSizeWidth(rs.getDouble("MIN_SIZE_WIDTH"));
		if (list.contains("GX_ID"))
			current.setGxId(rs.getLong("GX_ID"));
		if (list.contains("EXPRESS"))
			current.setExpress(rs.getString ("EXPRESS"));
		if (list.contains("JX_ID"))
			current.setJxId(rs.getLong("JX_ID"));
		if (list.contains("JT_ID"))
			current.setJtId(rs.getLong("JT_ID"));
		if (list.contains("SF_WX"))
			current.setSfWx(rs.getInt("SF_WX"));
		if (list.contains("BM_ID"))
			current.setBmId(rs.getLong("BM_ID"));
		if (list.contains("WX_ID"))
			current.setWxId(rs.getLong("WX_ID"));
		if (list.contains("BEI_ZHU"))
			current.setBeiZhu(rs.getString ("BEI_ZHU"));
		return current;
	}
	
	/**
	 * 从工艺ID获取工艺
	 */
	public GongYi getGongYiByID(long gyId) throws DaoException{
		logger.debug("GongYi getGongYiByID("+gyId+") - run"); 
		return (GongYi)queryObject("SELECT GY_ID,NAME,GYLB_ID,DW_ID,PRICE,QIDIAN_NUM,QIDIAN_PRICE,MAX_KD,MAX_SIZE_LENGTH,MAX_SIZE_WIDTH,MIN_SIZE_LENGTH,MIN_SIZE_WIDTH,GX_ID,EXPRESS,JX_ID,JT_ID,SF_WX,BM_ID,WX_ID,BEI_ZHU  FROM SH_XT_GONG_YI_T WHERE GY_ID=" + gyId);
	}
	
	/**
	 * 添加工艺
	 */
	public int addGongYi(GongYi obj) throws DaoException {
		logger.debug("addGongYi(GongYi) - start"); 

		int result = 0;
		List paramList = new ArrayList();
		
		long gyId= querySequence("SEQ_SH_XT_GONG_YI_T");
		paramList.add(gyId);		
		paramList.add(obj.getName().trim());
		paramList.add(obj.getGylbId());
		paramList.add(obj.getDwId());
		paramList.add(obj.getPrice());
		paramList.add(obj.getQidianNum());
		paramList.add(obj.getQidianPrice());
		paramList.add(obj.getMaxKd());
		paramList.add(obj.getMaxSizeLength());
		paramList.add(obj.getMaxSizeWidth());
		paramList.add(obj.getMinSizeLength());
		paramList.add(obj.getMinSizeWidth());
		paramList.add(obj.getGxId());
		paramList.add(obj.getExpress().trim());
		paramList.add(obj.getJxId());
		paramList.add(obj.getJtId());
		paramList.add(obj.getSfWx());
		paramList.add(obj.getBmId());
		paramList.add(obj.getWxId());
		paramList.add(obj.getBeiZhu().trim());
		
		String sql = "INSERT INTO SH_XT_GONG_YI_T (GY_ID,NAME,GYLB_ID,DW_ID,PRICE,QIDIAN_NUM,QIDIAN_PRICE,MAX_KD,MAX_SIZE_LENGTH,MAX_SIZE_WIDTH,MIN_SIZE_LENGTH,MIN_SIZE_WIDTH,GX_ID,EXPRESS,JX_ID,JT_ID,SF_WX,BM_ID,WX_ID,BEI_ZHU) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("添加工艺失败!\n 工艺信息" + BeanUtil.getBeanLog(obj));
		}

		logger.debug("addGongYi(GongYi) - end"); 
		return result;
	}
	
	/**
	 * 删除工艺
	 */
	public int delGongYi(GongYi obj) throws DaoException {
        logger.debug("delGongYi(GongYi) - start"); 
		
		int result = 0;
		List paramList = new ArrayList();
		
		paramList.add(obj.getGyId());
	 
		String sql = "DELETE FROM SH_XT_GONG_YI_T WHERE GY_ID=?";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("删除工艺失败!\n 工艺信息" + BeanUtil.getBeanLog(obj));
		}
        
		logger.debug("delGongYi(GongYi) - end"); 
		return result;
	}
	
	/**
	 * 返回工艺的总数
	 */
	public int getTotalGongYis() throws DaoException {
		logger.debug("getTotalGongYis()  - run"); 
		
		String sql = "SELECT COUNT(1) FROM SH_XT_GONG_YI_T";
		return queryInt(sql);
	}


	/**
	 * 查询工艺并排序
	 */
	public List getGongYis(String property, String sortOrder,int limit, int offset) throws DaoException {
		logger.debug("List getGongYis  ("+property+","+ sortOrder+","+ limit+","+  offset+")- run"); 
		
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT GY_ID,NAME,GYLB_ID,DW_ID,PRICE,QIDIAN_NUM,QIDIAN_PRICE,MAX_KD,MAX_SIZE_LENGTH,MAX_SIZE_WIDTH,MIN_SIZE_LENGTH,MIN_SIZE_WIDTH,GX_ID,EXPRESS,JX_ID,JT_ID,SF_WX,BM_ID,WX_ID,BEI_ZHU  FROM SH_XT_GONG_YI_T");
		if (property != null && sortOrder != null) {
			sb.append(" ORDER BY ");
			sb.append(" ");
			sb.append(property);
			sb.append(" ");
			sb.append(sortOrder);
		} else{
			sb.append(" ORDER BY GY_ID DESC ");
		}
		
		logger.debug("List getGongYis(String property, String sortOrder,int limit, int offset)- end"); 
		return queryList(sb.toString(),limit,offset);
	}
	
	/**
	 * 更新工艺
	 */
	public int updateGongYi(GongYi obj) throws DaoException {
		logger.debug("updateGongYi(GongYi) - start"); 
		
		int result = 0;
		List paramList = new ArrayList();
		
		paramList.add(obj.getName().trim());
		paramList.add(obj.getGylbId());
		paramList.add(obj.getDwId());
		paramList.add(obj.getPrice());
		paramList.add(obj.getQidianNum());
		paramList.add(obj.getQidianPrice());
		paramList.add(obj.getMaxKd());
		paramList.add(obj.getMaxSizeLength());
		paramList.add(obj.getMaxSizeWidth());
		paramList.add(obj.getMinSizeLength());
		paramList.add(obj.getMinSizeWidth());
		paramList.add(obj.getGxId());
		paramList.add(obj.getExpress().trim());
		paramList.add(obj.getJxId());
		paramList.add(obj.getJtId());
		paramList.add(obj.getSfWx());
		paramList.add(obj.getBmId());
		paramList.add(obj.getWxId());
		paramList.add(obj.getBeiZhu().trim());
		
		paramList.add(obj.getGyId());
		
		String sql = "UPDATE SH_XT_GONG_YI_T SET NAME=?,GYLB_ID=?,DW_ID=?,PRICE=?,QIDIAN_NUM=?,QIDIAN_PRICE=?,MAX_KD=?,MAX_SIZE_LENGTH=?,MAX_SIZE_WIDTH=?,MIN_SIZE_LENGTH=?,MIN_SIZE_WIDTH=?,GX_ID=?,EXPRESS=?,JX_ID=?,JT_ID=?,SF_WX=?,BM_ID=?,WX_ID=?,BEI_ZHU=?  WHERE GY_ID=?";
		result = dao.prepareUpdate(sql, paramList);
		if (result != 1){
			throw new DaoException("更新工艺失败!\n 工艺信息" + BeanUtil.getBeanLog(obj));
		}
		
		logger.debug("updateGongYi(GongYi) - end"); 
		return result;
	}
}
